package com.in.mobilerechargedth;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import com.in.mobilerechargedth.adapter.SpinnerAdapter;
import com.in.mobilerechargedth.api.TransponderApi;
import com.in.mobilerechargedth.constant.ConstantVariables;
import com.in.mobilerechargedth.model.TransponderModel;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;

public class SixthScreenActivity extends BaseActivity {

    Button btnGo;
    Spinner spnFrequency;

    private List<String> listFrequency;

    public static List<TransponderModel> listTransponderModel;
    Button backbtn;

    int operatorId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sixth_screen);
        String strOperatorId = getIntent().getStringExtra(ConstantVariables.OPERATOR_ID);
        operatorId = Integer.parseInt(strOperatorId);
        listFrequency = new ArrayList<String>();
        backbtn = (Button) findViewById(R.id.backbtn);
        backbtn.setVisibility(View.VISIBLE);
        backbtn.setOnClickListener(this);
        btnGo = (Button) findViewById(R.id.btnGo);
        spnFrequency = (Spinner) findViewById(R.id.spnFrequency);

        //
        btnGo.setOnClickListener(this);

        new TransponderTask().execute();
        setupAdmob();
        share();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        super.onClick(v);
        switch (v.getId()) {
        case R.id.btnGo:

            if (spnFrequency.getSelectedItem() != null) {
                Intent i = new Intent(SixthScreenActivity.this,
                        SixthTransponderDetailScreenActivity.class);
                i.putExtra(ConstantVariables.FREQUENCY_NAME, spnFrequency
                        .getSelectedItem().toString());
                i.putExtra(ConstantVariables.OPERATOR_ID, operatorId + "");
                startActivity(i);
            }

            Log.e("Operator_id second put", operatorId + "");

            break;
        case R.id.backbtn:
            this.onBackPressed();
            break;
        }
    }

    /**
     * Async Task to proceed Operator.
     */
    private class TransponderTask extends
            AsyncTask<Void, Void, List<TransponderModel>> {

        private ProgressDialog mProgress;

        @Override
        protected void onPreExecute() {
            mProgress = new ProgressDialog(SixthScreenActivity.this);
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgress.setMessage("Loading...");
            mProgress.setCanceledOnTouchOutside(false);
            // Check press back key when showing dialog.
            mProgress.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
            mProgress.show();

            super.onPreExecute();
        }

        @Override
        protected List<TransponderModel> doInBackground(Void... params) {
            TransponderApi api = new TransponderApi();
            List<TransponderModel> data = null;
            try {
                data = api.getTransponder(operatorId);
            } catch (Exception e) {
                Log.e("getCircle", e.getMessage());
            }

            return data;
        }

        @Override
        protected void onPostExecute(List<TransponderModel> result) {
            super.onPostExecute(result);

            if (result != null) {
                listTransponderModel = result;
                for (TransponderModel transponderModel : result) {
                    int flag = 0;
                    for (int i = 0; i < listFrequency.size(); i++) {
                        if (listFrequency.get(i).equals(
                                transponderModel.getFrequency())) {
                            flag = 1;
                        }
                    }
                    Log.e("getCircle", transponderModel.getFrequency());
                    if (flag == 0) {
                        listFrequency.add(transponderModel.getFrequency());
                    }

                }
                Collections.sort(listFrequency);
                SpinnerAdapter frequencyAdapter = new SpinnerAdapter(
                        SixthScreenActivity.this,
                        android.R.layout.simple_spinner_item, listFrequency);
                frequencyAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spnFrequency.setAdapter(frequencyAdapter);
            }
            if (mProgress != null) {
                mProgress.dismiss();
                mProgress = null;
            }

        }

    }
}
